<?php

namespace app\admin\controller;

use think\facade\Env;

class Home extends Base
{
    public function index()
    {
        return view();
    }

    public function cateList()
    {
        $cateList = model('Cate')->paginate(10);
        $viewData = [
            'cateList' => $cateList
        ];
        $this->assign($viewData);
        return view('catelist');
    }

    public function cateAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Cate')->add($data);
            if ($result == 1) {
                $this->success('栏目添加成功！','admin/home/catelist');
            }else {
                $this->error($result);
            }
        }
        return view('cateadd');
    }

    public function cateEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Cate')->edit($data);
            if ($result == 1) {
                $this->success('栏目修改成功！', 'admin/home/catelist');
            }else {
                $this->error($result);
            }
        }
        $cateInfo = model('Cate')->where('id', input('id'))->find();
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this->assign($viewData);
        return view('cateedit');
    }

    public function cateDel()
    {
        $result = model('Cate')->where(input('post.'))->delete();
        if ($result) {
            $this->success('栏目删除成功！', 'admin/home/catelist');
        }else {
            $this->error('栏目删除失败！');
        }
    }

    public function articleList()
    {
        $articleList = model('Article')->with('cate')->paginate(10);
        $viewData = [
            'articleList' => $articleList
        ];
        $this->assign($viewData);
        return view('articlelist');
    }

    public function articleAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Article')->add($data);
            if ($result == 1) {
                $this->success('文章添加成功！', 'admin/home/articlelist');
            }else {
                $this->error($result);
            }
        }
        $cateList = model('Cate')->select();
        $viewData = [
            'cateList' => $cateList
        ];
        $this->assign($viewData);
        return view('articleadd');
    }

    public function articleEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Article')->edit($data);
            if ($result == 1) {
                $this->success('文章修改成功！', 'admin/home/articlelist');
            }else {
                $this->error($result);
            }
        }
        $articleInfo = model('Article')->where('id', input('id'))->find();
        $cateList = model('Cate')->select();
        $viewData = [
            'articleInfo' => $articleInfo,
            'cateList' => $cateList
        ];
        $this->assign($viewData);
        return view('articleedit');
    }

    public function articleDel()
    {
        $result = model('Article')->where('id', input('id'))->delete();
        if ($result) {
            $this->success('文章删除成功！', 'admin/home/articlelist');
        }else {
            $this->error('文章删除失败！');
        }
    }

    public function memberList()
    {
        $memberList = model('Member')->paginate(10);
        $viewData = [
            'memberList' => $memberList
        ];
        $this->assign($viewData);
        return view('memberlist');
    }

    public function memberAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Member')->add($data);
            if ($result == 1) {
                $this->success('会员添加成功！', 'admin/home/memberlist');
            }else {
                $this->error($result);
            }
        }
        return view('memberadd');
    }

    public function memberEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Member')->edit($data);
            if ($result == 1) {
                $this->success('会员修改成功！', 'admin/home/memberlist');
            }else {
                $this->error($result);
            }
        }
        $memberInfo = model('Member')->where('id', input('id'))->find();
        $viewData = [
            'memberInfo' => $memberInfo
        ];
        $this->assign($viewData);
        return view('memberedit');
    }

    public function memberDel()
    {
        $result = model('Member')->where('id', input('id'))->delete();
        if ($result) {
            $this->success('会员删除成功！', 'admin/home/memberlist');
        }else {
            $this->error($result);
        }
    }

    public function adminList()
    {
        $adminList = model('Admin')->paginate(10);
        $viewData = [
            'adminList' => $adminList
        ];
        $this->assign($viewData);
        return view('adminlist');
    }

    public function adminAdd()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Admin')->add($data);
            if ($result == 1) {
                $this->success('管理员添加成功！', 'admin/home/adminlist');
            }else {
                $this->error($result);
            }
        }
        return view('adminadd');
    }

    public function adminEdit()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('Admin')->edit($data);
            if ($result == 1) {
                $this->success('管理员修改成功！', 'admin/home/adminlist');
            }else {
                $this->error($result);
            }
        }
        $adminInfo = model('Admin')->where('id', input('id'))->find();
        $viewData = [
            'adminInfo' => $adminInfo
        ];
        $this->assign($viewData);
        return view('adminedit');
    }

    public function adminDel()
    {
        $result = model('Admin')->where('id', input('id'))->delete();
        if ($result) {
            $this->success('管理员删除成功！', 'admin/home/adminlist');
        }else {
            $this->error($result);
        }
    }

    public function systemSet()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            $result = model('System')->edit($data);
            if ($result == 1) {
                $this->success('网站信息修改成功！', 'admin/home/index');
            }else {
                $this->error($result);
            }
        }
        $webInfo = model('System')->find();
        $viewData = [
            'webInfo' => $webInfo
        ];
        $this->assign($viewData);
        return view('systemset');
    }
}
